<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <div style="margin-bottom:10px">
          <!-- TODO: 单独封装搜索组件 -->
          <search
            @searchValue="handleSearchValue"
          />
        </div>
        <!-- TODO: 单独封装树形组件 -->
        <el-tree @select="getDaat" />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/components/addEmployee')">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- TODO: 表格组件（不用封装, 如果封装，难度偏大，但是可以挑战，实现就好） -->
        <el-table :data="employeeTable">
          <el-table-column prop="staffPhoto" label="头像">
            <template #default="{row}">
              <img v-if="row.staffPhoto" class="username" :src="row.staffPhoto">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" />
          <el-table-column prop="workNumber" label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span>{{ row.formOfEmployment===1? "正式":"非正式" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-row>
                <el-button type="text" size="mini" @click="$router.push(`/employee/components/addEmployee/${row.id}`)">查看</el-button>
                <el-button type="text" size="mini">角色</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="delClick(row.id)"
                >
                  <el-button slot="reference" type="text" size="mini">删除</el-button>
                </el-popconfirm>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- TODO: 单独封装分页组件 -->
        <pageVue
          :querry-parmas="querryParmas"
          :total-table="totalTable"
          class="page"
          @CurrentChange="CurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getEmployeeTableAPI, delemployeeTableAPI } from '@/api/employee'
import Search from './components/Search.vue'
import ElTree from './components/elTree.vue'
import pageVue from './components/pageVue.vue'
export default {
  name: 'Employee',
  components: {
    Search,
    ElTree,
    pageVue
  },

  data() {
    return {
      employeeTable: [],
      querryParmas: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      totalTable: 0

    }
  },
  created() {
    this.getEmployeeTable()
  },
  methods: {
    handleSearchValue(keyword) {
      this.querryParmas.page = 1
      this.querryParmas.keyword = keyword
      console.log(keyword)
      this.getEmployeeTable()
    },
    getDaat(id) {
      this.querryParmas.page = 1
      this.querryParmas.departmentId = id
      this.getEmployeeTable()
    },
    // 获取用户列表
    async getEmployeeTable() {
      const { total, rows } = await getEmployeeTableAPI(this.querryParmas)
      this.employeeTable = rows
      this.totalTable = total
    },
    // 删除用户
    async delClick(id) {
      await delemployeeTableAPI(id)
      this.$message.success('删除成功')
      this.getEmployeeTable()
    },
    // 页码切换
    CurrentChange(val) {
      this.querryParmas.page = val
      this.getEmployeeTable()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    .page{
      display: flex;
      justify-content: end;
    }
  }
}

</style>
